﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TABARTE
    {
        #region ==CART===========
        public static readonly DependencyProperty CARTProperty = DependencyProperty.Register("CART",
            typeof(string), typeof(TABARTE),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCARTChanged));
        public string CART
        {
            get
            {
                return (string)GetValue(CARTProperty);
            }
            set
            {
                SetValue(CARTProperty, value);
                Load();
            }
        }
        private static void OnCARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TABARTE;
            if (src != null)
            {
                src.CART = (string)e.NewValue;
             }
        }
        #endregion  

        #region ==dwABE===========
        public static readonly DependencyProperty dwABEProperty = DependencyProperty.Register("dwABE",
            typeof(DataRow), typeof(TABARTE),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwABEChanged));
        public DataRow dwABE
        {
            get
            {
                return (DataRow)GetValue(dwABEProperty);
            }
            set
            {
                SetValue(dwABEProperty, value);
            }
        }
        private static void OndwABEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TABARTE;
            if (src != null)
            {
                src.dwABE = (DataRow)e.NewValue;
            }
        }
        #endregion 

        public TABARTE()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            MV = new VABARTE();
            DataContext = MV;
             InitializeComponent(); 
        }
       
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {

            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded) return;
            base.Window_Loaded(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwABE = dwMaster; 
        } 
        public void Load()
        {
            if (CART.Length < 23)
                return;
            dwData =TBMain.NewRow();
            dwData["GSMANDANT"] = app.Mandant;
            dwData["GSART"] = Helpers.Helper.Left(CART, 20);
            dwData["GSVAR"] = CART.Substring(20, 3);
            SResult =((VABARTE)MV).QueryABARTE(dwData, 1);
            MergeData(SResult,true);
        }
    }
}
